var express = require('express');
var router = express.Router();
const { getOne, getAll, exec } = require('../db');

// 引入JWT
var jwt = require('jsonwebtoken');
//引入密钥
const { TOKEN_KEY, TIME } = require('../plugins/config.js');

/**
 * 获取所有用户信息
 * GET /users
 */
router.get('/', async (req, res) => {
	const { page, size, keyword } = req.query;
	const offset = (page - 1) * size;
	if (!keyword) {
		let sql = `select * from all_user limit ${offset},${size}`;
		const data = await getAll(sql);
		sql = `select count(*) as total from all_user`;
		const { total } = await getOne(sql);
		res.send({
			code: 0,
			message: '获取所有用户成功',
			result: {
				total,
				data,
			},
		});
	} else {
		let sql = `select * from all_user where user_name like '%${keyword}%' limit ${offset},${size}`;
		const data = await getAll(sql);
		sql = `select count(*) as total from all_user where user_name like '%${keyword}%'`;
		const { total } = await getOne(sql);
		res.send({
			code: 0,
			message: '获取所有用户成功',
			result: {
				total,
				data,
			},
		});
	}
});

/**
 * 根据id获取单个用户信息
 * GET /users/:id
 */
router.get('/:id', async (req, res, next) => {
	const { id } = req.params;
	// console.log(id);

	let sql = `select * from all_user where id=${id}`;
	const data = await getOne(sql);
	res.send({
		code: 0,
		message: '获取用户信息成功',
		result: data,
	});
});

/* 用户登录
 * POST /login
 */
router.post('/login', async (req, res, next) => {
	const { user_name, user_password } = req.body;
	let sql = `select * from all_user where user_name='${user_name}' and user_password='${user_password}'`;
	const data = await getOne(sql);
	if (data) {
		//创建token   jwt.sign方法第一个参数可以存储信息 ,第二个参数存储token密钥 第三个是token过期时间
		let token = jwt.sign({ user_name, user_password }, TOKEN_KEY, {
			expiresIn: TIME,
		});
		res.send({
			code: 0,
			message: '登录成功',
			result: { data, token },
		});
	} else {
		res.send({
			code: 1,
			message: '用户名或密码错误',
		});
	}
});

/**
 * 添加用户信息
 * POST /users
 */
router.post('/', async (req, res, next) => {
	// 1.解析参数
	const { user_name, user_password, user_role } = req.body;
	const repeatName = await getOne(
		`select * from all_user where user_name='${user_name}'`
	);
	// console.log(repeatName);
	if (!repeatName) {
		if (user_role) {
			let sql = `insert into all_user(user_name,user_password,user_role) values ('${user_name}','${user_password}','${user_role}')`;
			await exec(sql);
			res.send({
				code: 0,
				message: '添加用户成功',
			});
		} else {
			let sql = `insert into all_user(user_name,user_password) values ('${user_name}','${user_password}')`;
			await exec(sql);
			res.send({
				code: 0,
				message: '添加用户成功',
			});
		}
	} else {
		res.send({
			code: 1,
			message: '用户名已存在',
		});
	}
	// 2.操作数据库
	// 3.执行sql，返回结果
});
/**
 * 修改用户数据
 * PUT /users/:id
 */
router.put('/:id', async (req, res, next) => {
	const { id } = req.params;
	const { user_name, user_password, user_islogin } = req.body;
	// console.log(user_name, user_password, user_islogin);
	if (user_name) {
		let sql = `update all_user set user_name='${user_name}',user_password='${user_password}' where id = ${id}`;
		await exec(sql);
		res.send({
			code: 0,
			message: '更新用户成功',
		});
	} else {
		let sql = `update all_user set user_islogin='${user_islogin}' where id = ${id}`;
		await exec(sql);
		res.send({
			code: 0,
			message: '更新用户状态成功',
		});
	}
});
/**
 * 根据id删除用户
 * DELETE /users/:id
 */
router.delete('/:id', async (req, res, next) => {
	const { id } = req.params;
	let sql = `delete from all_user where id in (${id})`;
	await exec(sql);
	res.send({
		code: 0,
		message: '删除用户数据成功',
	});
});

module.exports = router;
